package com.persicadev.khayyam;

import java.util.Random;

import com.persicadev.khayyam.R;
import com.persicadev.khayyam.IPoemDb.Poem;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.DataSetObserver;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class BrowseActivity extends Activity implements OnTouchListener, OnItemClickListener, OnGestureListener, SpinnerAdapter {

	private GestureDetector mGestureDetector;
	private Gallery mGallery;
	private SharedPreferences sp;
	String PoemText="";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browsepoems);
		sp = getSharedPreferences(KhayyamActivity.PREFERENCES, MODE_PRIVATE);
		mGestureDetector = new GestureDetector(this, this);
		mGallery = (Gallery) findViewById(R.id.browser);
		mGallery.setAdapter(this);
		mGallery.setOnItemClickListener(this);
		mGallery.setOnTouchListener(this);
		mGallery.setSelection(new Random().nextInt(KhayyamActivity.poemDb.totalPoems()), true);
	}

	public void onItemClick(AdapterView<?> parent, View child, int position, long id) {
		String key = String.valueOf(position).trim();
		if (sp.contains(key)) {
			child.setBackgroundColor(0);
			sp.edit().remove(key).commit();
		}
		else {
			child.setBackgroundColor(0x4400ff00);
			sp.edit().putInt(key, position).commit();
			child.setBackgroundColor(0x4400ff00);
		}
	}

	public boolean onTouch(View v, MotionEvent event) {
		mGestureDetector.onTouchEvent(event);
		if (event.getAction()==MotionEvent.ACTION_UP
				|| event.getAction()==MotionEvent.ACTION_CANCEL
				|| event.getAction()==MotionEvent.ACTION_OUTSIDE
				|| event.getAction()==MotionEvent.ACTION_DOWN)
			mGallery.onTouchEvent(event);
		return true;
	}

	public boolean onDown(MotionEvent e) {
		return true;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		return false;
	}

	public void onLongPress(MotionEvent e) { }

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		float distance = e2.getX() - e1.getX();
		if (Math.abs(distance)>20) {
			mGallery.onTouchEvent(e2);
		}
		return true;
	}

	public void onShowPress(MotionEvent e) { }

	public boolean onSingleTapUp(MotionEvent e) {
		return true;
	}

	public int getCount() {
		return KhayyamActivity.poemDb.totalPoems();
	}


	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {
		return IGNORE_ITEM_VIEW_TYPE;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		FrameLayout view = (FrameLayout) View.inflate(this, R.layout.poemitem, null);
		//((RobaeiView) view.findViewById(R.id.item_poem)).setPoem(KhayyamActivity.poemDb.getPoem(position),true);
		((RobaeiView) view.findViewById(R.id.item_poem)).setPoem(KhayyamActivity.poemDb.getPoem(position),false);
		TextView text = (TextView) view.findViewById(R.id.item_id);
		text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/BZar.ttf"));
		text.setText(PersianReshape.reshape(""+(position+1)));
		if (sp.contains(String.valueOf(position).trim())) view.setBackgroundColor(0x4400ff00);
	    view.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));
	    Poem mPoem = KhayyamActivity.poemDb.getPoem(position-1);
	    PoemText=mPoem.getVerse(0).getPart1().toString()+"\n"+mPoem.getVerse(0).getPart2().toString()+"\n\n"+mPoem.getVerse(1).getPart1().toString()+"\n"+mPoem.getVerse(1).getPart2().toString();
	    ImageButton btnShare = (ImageButton)view.findViewById(R.id.btnShare);
	    btnShare.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
		        sharingIntent.setType("text/plain");
		        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
		        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, PoemText);
		        
		        startActivity(Intent.createChooser(sharingIntent, PersianReshape.reshape("اشتراک گذاری")));
			}
		});
		return view;
	}

	public int getViewTypeCount() {
		return 1;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isEmpty() {
		return false;
	}

	public void registerDataSetObserver(DataSetObserver observer) { }

	public void unregisterDataSetObserver(DataSetObserver observer) { }

	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		return null;
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}

}
